package com.assistant.framework;

import lombok.Data;
import org.springframework.data.domain.Pageable;

import java.util.List;

@Data
public class PageResult<T> {

    private int totalCount;
    private int totalPage;
    private int currentPage;
    private int start;
    private int limit;
    private List<T> items;

    public PageResult(int totalCount, int totalPage, int currentPage, int start, int limit, List<T> items) {
        super();
        this.totalCount = totalCount;
        this.totalPage = totalPage;
        this.currentPage = currentPage;
        this.start = start;
        this.limit = limit;
        this.items = items;
    }

    public PageResult(List<T> items, Pageable pageable, long totalCount) {
        super();
        this.items = items;
        this.start = Long.valueOf(pageable.getOffset()).intValue();
        this.limit = pageable.getPageSize();
        this.currentPage = pageable.getPageNumber();
        this.totalCount = Long.valueOf(totalCount).intValue();
    }

    public PageResult(List<T> items, int start, int limit, int totalCount) {
        this.items = items;
        this.start = start;
        this.limit = limit;
        this.totalCount = totalCount;
        if (limit == 0) {
            this.totalPage = 1;
            this.currentPage = 1;
        } else {
            this.totalPage = totalCount / limit + (totalCount % limit > 0 ? 1 : 0);
            this.currentPage = start / limit + 1;
        }
    }
}